package cn.iocoder.yudao.module.mini.controller.admin.technician;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mini.controller.admin.technician.vo.*;
import cn.iocoder.yudao.module.mini.dal.dataobject.technician.TechnicianDO;
import cn.iocoder.yudao.module.mini.convert.technician.TechnicianConvert;
import cn.iocoder.yudao.module.mini.service.technician.TechnicianService;

@Tag(name = "管理后台 - 技师")
@RestController
@RequestMapping("/mini/technician")
@Validated
public class TechnicianController {

    @Resource
    private TechnicianService technicianService;

    @PostMapping("/create")
    @Operation(summary = "创建技师")
    @PreAuthorize("@ss.hasPermission('mini:technician:create')")
    public CommonResult<Long> createTechnician(@Valid @RequestBody TechnicianCreateReqVO createReqVO) {
        return success(technicianService.createTechnician(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新技师")
    @PreAuthorize("@ss.hasPermission('mini:technician:update')")
    public CommonResult<Boolean> updateTechnician(@Valid @RequestBody TechnicianUpdateReqVO updateReqVO) {
        technicianService.updateTechnician(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除技师")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mini:technician:delete')")
    public CommonResult<Boolean> deleteTechnician(@RequestParam("id") Long id) {
        technicianService.deleteTechnician(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得技师")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mini:technician:query')")
    public CommonResult<TechnicianRespVO> getTechnician(@RequestParam("id") Long id) {
        TechnicianDO technician = technicianService.getTechnician(id);
        return success(TechnicianConvert.INSTANCE.convert(technician));
    }

    @GetMapping("/list")
    @Operation(summary = "获得技师列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mini:technician:query')")
    public CommonResult<List<TechnicianRespVO>> getTechnicianList(@RequestParam("ids") Collection<Long> ids) {
        List<TechnicianDO> list = technicianService.getTechnicianList(ids);
        return success(TechnicianConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得技师分页")
    @PreAuthorize("@ss.hasPermission('mini:technician:query')")
    public CommonResult<PageResult<TechnicianRespVO>> getTechnicianPage(@Valid TechnicianPageReqVO pageVO) {
        PageResult<TechnicianDO> pageResult = technicianService.getTechnicianPage(pageVO);
        return success(TechnicianConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出技师 Excel")
    @PreAuthorize("@ss.hasPermission('mini:technician:export')")
    @OperateLog(type = EXPORT)
    public void exportTechnicianExcel(@Valid TechnicianExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TechnicianDO> list = technicianService.getTechnicianList(exportReqVO);
        // 导出 Excel
        List<TechnicianExcelVO> datas = TechnicianConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "技师.xls", "数据", TechnicianExcelVO.class, datas);
    }

}
